package com.ghsc.ghxc.jsb.ui.integral.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.config.AppConfig
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.BarRefreshListBinding
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketViewModel
import com.ghsc.ghxc.jsb.ui.integral.adapter.IntegralCommodityAdapter
import com.ghsc.ghxc.jsb.ui.integral.bean.HomeIntegralBean
import com.ghsc.ghxc.jsb.view.NotDataView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral.activity
 * 类名：CommodityListActivity
 * 创建时间：2024/5/21 16:22
 * 创建人 @author： xiaolin
 * 描述 ：Todo...商品列表
 */
class CommodityListActivity : BaseVMActivity<IntegralSupermarketViewModel, BarRefreshListBinding>()  {
    override fun attachLayoutRes(): Int {
       return R.layout.bar_refresh_list
    }

    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
        return IntegralSupermarketViewModel::class.java
    }

    // 商品列表
    //屏幕的宽度
    var widthPixels= AppConfig.getApplication().resources.displayMetrics.widthPixels
    //屏幕的密度
    var density= AppConfig.getApplication().resources.displayMetrics.density
    private val mAdapter: IntegralCommodityAdapter by lazy { IntegralCommodityAdapter((widthPixels/density)) }
    var mList = arrayListOf<HomeIntegralBean>()
    var keyword =""
    var bundle: Bundle?=null
    private var notDataView: View? = null
    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        bundle=intent.extras
        keyword=bundle!!.getString("keyword","")
        binding.bar.topTitle.text="商品列表"
        binding.bar.topLeftView.setSingleClickListener { finish() }

        //
        binding.rvList.layoutManager = StaggeredGridLayoutManager(2,
            StaggeredGridLayoutManager.VERTICAL)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)
        notDataView= NotDataView.notDataView(this, binding.rvList,binding.refreshLayout,"暂无商品数据")
        initRefreshLayout()
        pageNo=1
        getListData()
    }
    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                //分类
                pageNo=1
                getListData()

            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }
    fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["category"] =keyword
        map["pindex"] =pageNo
        map["psize"] =20
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onGoodListMethod(it1) }
    }

    override fun initClickListener() {
        super.initClickListener()
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = mAdapter.data[position]
            loge("position=item="+bean.toString())
            startKtxActivity<IntegralGoodsDetailActivity>(
                value =  "pid" to mAdapter.getItem(position).pid
            )
        }
    }
    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@CommodityListActivity) {
                dismissLoading()
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(it)
            }
            againLoginMsg.observe(this@CommodityListActivity) {
                dismissLoading()
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()

            }
            onGoodListData.observe(this@CommodityListActivity){
                // 首页列表
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()){
                        mAdapter.replaceData(it.data.list)
                        mAdapter.setEmptyView(notDataView!!)
                    }
                    else {
                        mAdapter.replaceData(it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }
        }
    }


}